package main

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"github.com/garyburd/redigo/redis"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"github.com/robfig/cron"
	"log"
	"os"
)

var (
	conf config
	pool   *redis.Pool
	engine *xorm.Engine
	imgUrl = [4]string{"http://img6.isou365.cn", "http://img7.isou365.cn", "http://img8.isou365.cn", "http://img9.isou365.cn"}
)

const (
	MaxOpenDbConnect int = 50
	MaxIdleConn int = 5
	MaxOpenGoRoutine = 20
	BusinessArticle = 1
)

func main() {

	if len(os.Args) <= 1 {
		fmt.Println("param error")
		os.Exit(2)
	}

	if _, err := toml.DecodeFile("./conf/config.toml", &conf); err != nil {
		log.Fatal("toml decode conf err : ", err.Error())
		return
	}

	file, err := os.OpenFile(conf.Log.File, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
	if err != nil {
		log.Fatal("open log failed: ", err)
	}
	defer file.Close()
	log.SetOutput(file)

	redisAddr := fmt.Sprintf("%s:%s", conf.Redis.Host, conf.Redis.Port)
	pool = newRedisPool(redisAddr, conf.Redis.Password, conf.Redis.Database, conf.Redis.Timeout)
	defer pool.Close()

	mysqlDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s",
		conf.Mysql.User,
		conf.Mysql.Password,
		conf.Mysql.Host,
		conf.Mysql.Port,
		conf.Mysql.Database,
		conf.Mysql.Charset,
	)
	engine, err = xorm.NewEngine("mysql", mysqlDsn)
	if err != nil {
		log.Panic("open mysql failed : ", err)
	}
	defer engine.Close()

	engine.SetMaxIdleConns(MaxIdleConn)      // 连接池空闲数
	engine.SetMaxOpenConns(MaxOpenDbConnect) // 最大连接数

	engine.ShowSQL(conf.Log.SqlLog)
	engine.SetLogger(xorm.NewSimpleLogger(file))

	err = engine.Ping()
	if err != nil {
		log.Panic("mysql connect failed : ", err)
	}
	log.Println("Init Finished.")

	arg := os.Args[1]
	if arg == "cron" {
		fmt.Println("cron start")
		c := cron.New()
		c.AddFunc("0 0 10 * * *", func() {
			crawler()
		})
		c.AddFunc("0 0 14 * * *", func() {
			crawler()
		})

		c.Start()
	} else if arg == "check" {
		fmt.Println("check article start")
		go procBiz() // 审核文章
	} else if arg == "crawler" {
		fmt.Println("crawler article start")
		go procCrawler() // 采集详情
	} else if arg == "site" {
		fmt.Println("crawler site test")
		crawler()
	} else if arg == "unit" {
		unitTest() // 单元测试
	} else {
		fmt.Println("param error")
	}

	select {}
}

func crawler() {
	for key, val := range site() {
		fmt.Println(key, val)
		switch val.SiteType {
		case "易车":
			go yiCheListHandle(val.Url, val.CategoryId, val.ChildId, val.StartPage, val.endPage)
		case "汽车之家":
			go autoHomeListHandle(val.Url, val.CategoryId, val.ChildId, val.StartPage, val.endPage)
		case "搜狐汽车":
			go sohuListHandle(val.Url, val.CategoryId, val.ChildId)
		case "搜狐二手车":
			go sohuUsedCarListHandle(val.Url, val.CategoryId, val.ChildId, val.StartPage, val.endPage)
		}
	}
}

func site() []*Site {

	s := []*Site{}

	s = append(s, &Site{SiteType: "易车", Url: "http://news.bitauto.com/xinche/?pageindex=%d", CategoryId: 1, ChildId: 9, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "易车", Url: "http://news.bitauto.com/pingce/?pageindex=%d", CategoryId: 1, ChildId: 7, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "易车", Url: "http://news.bitauto.com/daogou/?pageindex=%d", CategoryId: 1, ChildId: 5, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "易车", Url: "http://news.bitauto.com/yongche/?pageindex=%d", CategoryId: 2, ChildId: 11, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "易车", Url: "http://news.bitauto.com/youji/?pageindex=%d", CategoryId: 3, ChildId: 17, StartPage: 1, endPage: 2})

	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/news/%d/#liststart", CategoryId: 1, ChildId: 9, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/advice/%d/#liststart", CategoryId: 1, ChildId: 5, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/drive/%d/#liststart", CategoryId: 1, ChildId: 7, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/use/%d/#liststart", CategoryId: 2, ChildId: 11, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/travels/%d/#liststart", CategoryId: 3, ChildId: 17, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "汽车之家", Url: "http://www.autohome.com.cn/tuning/%d/#liststart", CategoryId: 3, ChildId: 18, StartPage: 1, endPage: 2})

	s = append(s, &Site{SiteType: "搜狐汽车", Url: "http://auto.sohu.com/guoneixc/index.shtml", CategoryId: 1, ChildId: 9})
	s = append(s, &Site{SiteType: "搜狐汽车", Url: "http://auto.sohu.com/haiwxc/index.shtml", CategoryId: 1, ChildId: 9})
	s = append(s, &Site{SiteType: "搜狐汽车", Url: "http://auto.sohu.com/jinhd/index.shtml", CategoryId: 1, ChildId: 9})
	s = append(s, &Site{SiteType: "搜狐汽车", Url: "http://auto.sohu.com/xinchehuigu/index.shtml", CategoryId: 1, ChildId: 9})

	s = append(s, &Site{SiteType: "搜狐二手车", Url: "http://api.db.auto.sohu.com/restful/news/list/usedcar/358360521/%d/15.json?_=%d", CategoryId: 4, ChildId: 19, StartPage: 1, endPage: 2})
	s = append(s, &Site{SiteType: "搜狐二手车", Url: "http://api.db.auto.sohu.com/restful/news/list/usedcar/358360520/%d/15.json?_=%d", CategoryId: 4, ChildId: 19, StartPage: 1, endPage: 2})

	return s
}

// 单元测试

func unitTest() {

	// 采集
	//article(13020)

	//易车列表采集
	//yiCheListHandle("http://news.bitauto.com/xinche/?pageindex=%d", 1, 6, 1, 10000)

	// 易车详情采集
	//art := new(ClcwArticle)
	//art.FromUrl = "http://news.bitauto.com/zgpc/20170610/1606817175.html"
	//yiCheDetailHandle(art)

	// 汽车之家列表采集
	//autoHomeListHandle("http://www.autohome.com.cn/news/%d/#liststart", 1, 6, 1, 10000)

	// 汽车之家详情采集
	//art := new(ClcwArticle)
	//art.FromUrl = "http://www.autohome.com.cn/drive/201705/882577-12.html#pvareaid=102624"
	//autoHomeDetailHandle(art)

	//aa := new(ClcwArticle)
	//aa.FromUrl = "http://www.autohome.com.cn/drive/201705/902464.html#pvareaid=102624"
	//autoHomeDetailHandle(aa)
	//
	//bb := new(ClcwArticle)
	//bb.FromUrl = "http://www.autohome.com.cn/news/201705/902478.html#pvareaid=102624"
	//autoHomeDetailHandle(bb)
	//
	//cc := new(ClcwArticle)
	//cc.FromUrl = "http://www.autohome.com.cn/news/201705/902502.html#pvareaid=102624"
	//autoHomeDetailHandle(cc)

	// 搜狐列表采集
	//sohuListHandle("http://auto.sohu.com/guoneixc/index.shtml", 1, 6)

	// 搜狐详情采集
	//art := new(ClcwArticle)
	//art.FromUrl = "http://auto.sohu.com/20170510/n492417201.shtml"
	//sohuDetailHandle(art)

	// 搜狐二手车列表采集
	//sohuUsedCarListHandle("http://api.db.auto.sohu.com/restful/news/list/usedcar/358360521/%d/15.json?_=%d", 1, 6, 1, 1000)

	// 搜狐二手车详情采集
	//art := new(ClcwArticle)
	//art.FromUrl = "http://2sc.sohu.com/guide/20170314/n483248954.shtml"
	//sohuUsedCarDetailHandle(art)
}
